import { http } from "@/utils/http";
import type { ApiResponse } from "./user";

export type NotificationTabKey = "all" | "personal" | "group";

export interface NotificationTemplateInfo {
  id: number | null;
  name?: string | null;
  type?: string | null;
}

export interface NotificationListItem {
  id: number;
  message_id: number;
  title: string;
  content: string;
  status: number;
  status_label: string;
  is_read: boolean;
  read_time?: string | null;
  sent_at?: string | null;
  create_time?: string | null;
  target_type: string;
  target_id: number;
  route?: string;
  payload?: Record<string, any>;
  template?: NotificationTemplateInfo | null;
}

export interface NotificationPagination {
  total: number;
  page: number;
  page_size: number;
}

export interface NotificationListResponse {
  list: NotificationListItem[];
  pagination: NotificationPagination;
  unread: number;
}

export interface NotificationSummary {
  personal_unread: number;
  group_unread: number;
  total_unread: number;
}

export interface NotificationListParams {
  page?: number;
  page_size?: number;
  include_read?: number;
}

export interface MarkNotificationReadPayload {
  message_id: number;
  recipient_id: number;
}

const CATEGORY_ENDPOINT: Record<NotificationTabKey, string> = {
  all: "/user/notify/all",
  personal: "/user/notify/personal",
  group: "/user/notify/group"
};

export const fetchNotificationSummary = () => {
  return http.request<ApiResponse<NotificationSummary>>(
    "get",
    "/user/notify/summary"
  );
};

export const fetchNotificationList = (
  key: NotificationTabKey,
  params: NotificationListParams
) => {
  const endpoint = CATEGORY_ENDPOINT[key];
  return http.request<ApiResponse<NotificationListResponse>>("get", endpoint, {
    params
  });
};

export const markNotificationRead = (data: MarkNotificationReadPayload) => {
  return http.request<ApiResponse<NotificationListItem>>(
    "post",
    "/user/notify/read",
    {
      data
    }
  );
};
